﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Day
{
    public class TimeSharing
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        /// <summary>
        /// 查询的交易员
        /// </summary>
        public Models.Sys.Operator Operator { get; set; }
        /// <summary>
        /// 查询的日期
        /// </summary>
        public DateTime Pdate { get; set; }

        public string HPdate { get; set; }
        /// <summary>
        /// 今日交易数据
        /// </summary>
        public Models.View.OperatorDayChange OperatorDayChange { get; set; }
        /// <summary>
        /// 上个交易日交易数据
        /// </summary>
        public Models.View.OperatorDayChange PreOperatorDayChange { get; set; }
        /// <summary>
        /// 本周均值
        /// </summary>
        public Models.View.OperatorAvgWeek OperatorAvgWeek { get; set; }
        /// <summary>
        /// 上周均值
        /// </summary>
        public Models.View.OperatorAvgWeek PreOperatorAvgWeek { get; set; }
        /// <summary>
        /// 五日均值
        /// </summary>
        public Models.View.OperatorFiveDayAvg OperatorFiveDayAvg { get; set; }
        /// <summary>
        /// 上一个五日均值
        /// </summary>
        public Models.View.OperatorFiveDayAvg PreOperatorFiveDayAvg { get; set; }
        /// <summary>
        /// 页面脚本
        /// </summary>
        public string JS { get; set; }

        #region 定义一些使用到的接口
        private Interface.Sys.IOperator IOperator = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.View.IOperatorDayChange IOperatorDayChange = null;
        private Interface.View.IOperatorAvgWeek IOperatorAvgWeek = null;
        private Interface.View.IOperatorFiveDayAvg IOperatorFiveDayAvg = null;
        private Interface.View.IOperatorDayTradeInfo IOperatorDayTradeInfo = null;
        private Interface.View.IOperatorYieldInHoldTime IOperatorYieldInHoldTime = null;
        #endregion


        public TimeSharing(int uid, string queryDate, DB.DBManager db)
        {
            InitInterface(db);
            this.Operator = IOperator.GetModel(uid);

            string _year = queryDate.ToString().Substring(0, 4);
            string _month = queryDate.ToString().Substring(4, 2);
            string _day = queryDate.ToString().Substring(6, 2);
            string _pdate = _year + "-" + _month + "-" + _day;
            this.Pdate = Convert.ToDateTime(_pdate);
            this.HPdate = _pdate;

            Models.Sys.TradeHistory th = ITradeHistory.GetSettlementModel(queryDate);

            Models.Sys.TradeHistory preDay = ITradeHistory.GetPreviousTradingDay(th.Id);
            Models.Sys.TradeHistory preWeekLastDay = ITradeHistory.GetPreviousWeekLastTradingDay(th.Id);

            this.OperatorDayChange = IOperatorDayChange.GetOperatorDayChange(this.Operator.Id, th.Id);

            this.PreOperatorDayChange = IOperatorDayChange.GetOperatorDayChange(this.Operator.Id, preDay.Id);

            this.OperatorAvgWeek = IOperatorAvgWeek.GetOperatorAvgWeek(this.Operator.Id, th.YPdate, th.WPdate);

            this.PreOperatorAvgWeek = IOperatorAvgWeek.GetOperatorAvgWeek(this.Operator.Id, preWeekLastDay.YPdate, preWeekLastDay.WPdate);

            this.OperatorFiveDayAvg = IOperatorFiveDayAvg.GetOperatorFiveDayAvg(this.Operator.Id, th.Id);

            this.PreOperatorFiveDayAvg = IOperatorFiveDayAvg.GetOperatorFiveDayAvg(this.Operator.Id, preDay.Id);

            Models.View.OperatorDayTradeInfo info = IOperatorDayTradeInfo.GetOperatorDayTradeInfo(th.Id, this.Operator.Id, "billcount");
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.GetChartBillCountOnCCFX(info);
            var query = IOperatorYieldInHoldTime.GetOperatorYieldInHoldTime(th.Id, this.Operator.Id);
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.GetChartYieldOnCCFX(query);
            DisposeInterface();
        }

        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            IOperator = new Service.Sys.ServiceOperator(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IOperatorDayChange = new Service.View.ServiceOperatorDayChange(db);
            IOperatorAvgWeek = new Service.View.ServiceOperatorAvgWeek(db);
            IOperatorFiveDayAvg = new Service.View.ServiceOperatorFiveDayAvg(db);
            IOperatorDayTradeInfo = new Service.View.ServiceOperatorDayTradeInfo(db);
            IOperatorYieldInHoldTime = new Service.View.ServiceOperatorYieldInHoldTime(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            IOperator = null;
            ITradeHistory = null;
            IOperatorDayChange = null;
            IOperatorAvgWeek = null;
            IOperatorFiveDayAvg = null;
            IOperatorDayTradeInfo = null;
            IOperatorYieldInHoldTime = null;
            #endregion
        }
    }
}
